#用正则表达式匹配多个模式
'''
正则表达式匹配用法：（查询符合格式的字符串）
1.用import re导入正则表达式模块
2.用re.compile()函数创建一个Regex对象
3.向Regex对象的search()方法传入想查找的字符串，它返回一个Match对象
4.调用Match对象的group()方法，返回实际匹配文本的字符串。

'''

import re
NumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = NumRegex.search('My number is 415-666-4242')
print(mo.group(1))                  #415
print(mo.group(2))                  #666-4242
print(mo.group(0))                  #415-666-4242
print(mo.group())                   #415-666-4242
print(mo.groups())                  #('415', '666-4242') groups一次性获取所有分组
arecode,mainnumber = mo.groups()    #多重赋值
print(arecode)                      #415
print(mainnumber)                   #666-4242
print('\n')

#用\(……\)转义符打印出括号
NumRegex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)')
mo1 = NumRegex.search('My number is (415) 666-4242')
print(mo1.group(1))                #(415)
print(mo1.group(2))                #666-4242
print(mo1.group(0))                #(415) 666-4242
print(mo1.group())                 #(415) 666-4242
print('\n')




